package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginDTO;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.dto.UserSelectParam;
import cn.tedu.ivos.user.pojo.dto.UserUpdateParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserLeadVO;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public JsonResult login(UserLoginDTO userLoginDTO) {
        log.debug("用户登录，参数：{}",userLoginDTO);
        UserVO userVO = userMapper.selectByUsername(userLoginDTO.getUsername());
        if (userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginDTO.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return JsonResult.ok(userVO);
    }

    @Override
    public JsonResult save(UserSaveParam userSaveParam) {
        if (userSaveParam.getId()==null){
            UserVO userVO = userMapper.selectByUsername(userSaveParam.getUsername());
            if(userVO!=null){
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            User user = new User();
            BeanUtils.copyProperties(userSaveParam,user);
            user.setPassword("123456");
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            if(user.getId()==null){
                userMapper.saveUser(user);
                return JsonResult.ok();
            }else{
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
        }else {
            UserUpdateParam userUpdateParam = new UserUpdateParam();
            BeanUtils.copyProperties(userSaveParam,userUpdateParam);
            userUpdateParam.setUpdateTime(new Date());
            userMapper.update(userUpdateParam);
            return JsonResult.ok();
        }
    }

    @Override
    public JsonResult select(UserSelectParam userSelectParam) {
        List<UserVO> userVOList = userMapper.selectUser(userSelectParam);
        return JsonResult.ok(userVOList);
    }

    @Override
    public JsonResult selectLevel(Integer level) {
        List<UserLeadVO> userLeadVOList = userMapper.selectLead(level);
        return JsonResult.ok(userLeadVOList);
    }

    @Override
    public JsonResult update(UserUpdateParam userSaveParam) {
        if (userSaveParam.getUsername()==null|| userSaveParam.getUsername().equals("")){
            userSaveParam.setUpdateTime(new Date());
            userSaveParam.setPassword("123456");
            userMapper.update(userSaveParam);
        }
        return JsonResult.ok();
    }

    @Override
    public JsonResult del(Long id) {
        int i = userMapper.del(id);
        if (i!=0){
            return JsonResult.ok();
        }else {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }

    }
}
